package com.changgou.user.controller;

import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.goods.feign.SkuFeign;
import com.changgou.goods.pojo.Sku;
import com.changgou.user.config.TokenDecode;
import com.changgou.user.pojo.Collect;
import com.changgou.user.pojo.Footprint;
import com.changgou.user.service.FootprintService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/footprint")
public class FootprintController {

    @Autowired
    private FootprintService footprintService;

    @Autowired
    private TokenDecode tokenDecode;

    @Autowired
    private SkuFeign skuFeign;

    @GetMapping("/save2/{skuId}/{username}")
    @ResponseBody
    public void save2(@PathVariable("skuId") String skuId,@PathVariable("username") String username){

        footprintService.save(skuId,username);

    }

    @RequestMapping("/usernameBySkuId2")
    @ResponseBody
    public Result usernameBySkuId2(){
        String username = tokenDecode.getUserInfo().get("username");
        List<Footprint> list= footprintService.findBySkuId(username);
        List<Sku> skus = new ArrayList<>();
        for (Footprint footprint : list) {
            Result<Sku> byId = skuFeign.findById(footprint.getSkuId());
            skus.add(byId.getData());
        }
        Map map = new HashMap();
        map.put("list",skus);
        return new Result(true, StatusCode.OK,"dsfs",map);
    }
}
